package com.ych.modules.api.authorInfc.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.api.authorInfc.item.entity.AuditItemBiddingEntity;
import com.ych.modules.api.authorInfc.item.entity.BiddingVo;
import com.ych.modules.api.authorInfc.item.entity.QueryBiddingListVo;
import com.ych.modules.api.authorInfc.user.entity.AddItemEntity;
import com.ych.modules.base.dbEntity.TItemBiddingEntity;
import com.ych.modules.base.dbEntity.TUserItemEntity;
import com.ych.modules.base.service.ITUserInfoService;
import com.ych.modules.common.entity.query.QueryConcernItemEntity;
import com.ych.modules.common.entity.query.QueryUserItemEntity;
import com.ych.modules.common.service.UserItemService;
import com.ych.utils.TLMap;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/4
 * Time: 15:13
 * Description:
 */
@RestController("apiUserItemController")
@RequestMapping("/api/auth/item")
@Api(tags = {"API——用户项目相关"}, description = "API——用户项目相关", position = 2)
public class UserItemController {
    @Autowired
    private UserItemService   userItemService;
    @Autowired
    private ITUserInfoService itUserInfoService;

    @PostMapping("/getUserItem")
    @ApiOperation(value = "项目查询", notes = "项目查询")
    public R<TUserItemEntity> getUserItem(@RequestBody String itemId) {
        try {
            TUserItemEntity itemEntity = userItemService.getUserItem(itemId);
            return new R(0, "成功", itemEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listUserItem")
    @ApiOperation(value = "项目列表查询", notes = "项目列表查询")
    public R<Page<TUserItemEntity>> listUserItem(@RequestBody QueryUserItemEntity entity) {
        try {
            entity.setStatus("0");
            Page<TUserItemEntity> page = userItemService.listUserItem(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listPersonItem")
    @ApiOperation(value = "个人项目列表查询", notes = "项目列表查询")
    public R<Page<TUserItemEntity>> listPersonItem() {
        try {
            QueryUserItemEntity entity = new QueryUserItemEntity();
            entity.setUserId(TLMap.getUserId());
            Page<TUserItemEntity> page = userItemService.listUserItem(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/createItem")
    @ApiOperation(value = "创建项目", notes = "创建项目")
    public R<String> createItem(@RequestBody AddItemEntity entity) {
        try {
            TUserItemEntity itemEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TUserItemEntity.class);
            itemEntity.setUserId(TLMap.getUserId());
            //密码校验
            itUserInfoService.validatePayPassword(TLMap.getUserId(), entity.getPassword());
            userItemService.createItem(itemEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/itemViewCountIncrement")
    @ApiOperation(value = "项目浏览次数自增", notes = "项目浏览次数自增")
    public R<String> itemViewCountIncrement(@RequestBody String itemId) {
        try {
            userItemService.itemViewCountIncrement(itemId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/likeItem")
    @ApiOperation(value = "项目点赞/取消点赞", notes = "项目点赞/取消点赞")
    public R<String> likeItem(@RequestBody String itemId) {
        try {
            userItemService.likeItem(itemId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/concernItem")
    @ApiOperation(value = "项目关注/取消关注", notes = "项目关注/取消关注")
    public R<String> concernItem(@RequestBody String itemId) {
        try {
            userItemService.concernItem(itemId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/biddingItem")
    @ApiOperation(value = "项目竞价", notes = "项目竞价")
    public R<String> biddingItem(@RequestBody BiddingVo biddingVo) {
        try {
            TItemBiddingEntity entity = new TItemBiddingEntity();
            entity.setItemId(biddingVo.getItemId());
            entity.setGoldCoinAmount(biddingVo.getGoldCoinAmount());
            entity.setWechatNo(biddingVo.getWechatNo());
            //密码校验
            itUserInfoService.validatePayPassword(TLMap.getUserId(), biddingVo.getPassword());
            userItemService.biddingItem(entity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listItemBidding")
    @ApiOperation(value = "项目竞价列表查询", notes = "项目竞价列表查询")
    public R<Page<TItemBiddingEntity>> listItemBidding(@RequestBody QueryBiddingListVo vo) {
        try {
            if (StringUtils.isBlank(vo.getItemId())) {
                throw new RuntimeException("项目id不能为空");
            }
            TItemBiddingEntity entity = new TItemBiddingEntity();
            entity.setItemId(vo.getItemId());
            entity.setPage(vo.getPage());
            entity.setLimit(vo.getLimit());
            entity.setBiddingType("10017001");//10017001:竞价中
            Page<TItemBiddingEntity> page = userItemService.listItemBidding(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/removeItem")
    @ApiOperation(value = "删除项目", notes = "删除项目")
    public R<String> removeItem(@RequestBody String itemId) {
        try {
            userItemService.removeItem(itemId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listConcernItem")
    @ApiOperation(value = "我关注的项目列表查询", notes = "我关注的项目列表查询")
    public R<Page<TUserItemEntity>> listConcernItem(@RequestBody QueryConcernItemEntity entity) {
        try {
            entity.setUserId(TLMap.getUserId());
            Page<TUserItemEntity> page = userItemService.listConcernItem(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }


    @PostMapping("/auditItemBidding")
    @ApiOperation(value = "项目竞价审核", notes = "项目竞价审核")
    public R<String> auditItemBidding(@RequestBody AuditItemBiddingEntity vo) {
        try {
            if (StringUtils.isBlank(vo.getBiddingId())) {
                throw new RuntimeException("id不能为空");
            }
            userItemService.auditItemBidding(vo);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
